package mobi.weq.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mobi.weq.dao.HealthConditionDao;
import mobi.weq.model.HealthCondition;
import mobi.weq.model.User;
import mobi.weq.util.Util;

public class HealthConditionServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		User user = Util.getUserFromSession(req, resp);
		if (user != null) {
			req.setAttribute("user", user);
			req.getRequestDispatcher("/jsp/healthConditionSetting.jsp")
					.forward(req, resp);
		}
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		User user = Util.getUserFromSession(req, resp);
		if (user != null) {
			HealthCondition hc = new HealthCondition();
			// respiratory_infections
			String user_id = user.getUserId();
			hc.setUserId(user_id);
			String respiratory_infections = req
					.getParameter("respiratory_infections");
			if (respiratory_infections != null) {
				hc.setRespiratory_infections(respiratory_infections
						.equalsIgnoreCase("on"));
			}
			String lung_disease = req.getParameter("lung_disease");
			if (lung_disease != null) {
				hc.setLung_disease(lung_disease.equalsIgnoreCase("on"));
			}

			String asthma = req.getParameter("asthma");
			if (asthma != null) {
				hc.setAsthma(asthma.equalsIgnoreCase("on"));
			}

			String heart_disease = req.getParameter("heart_disease");
			if (heart_disease != null) {
				hc.setHeart_disease(heart_disease.equalsIgnoreCase("on"));
			}

			String allergies = req.getParameter("allergies");
			if (allergies != null) {
				hc.setAllergies(allergies.equalsIgnoreCase("on"));
			}
			
			HealthConditionDao sd = new HealthConditionDao();
			try {
				sd.updateHealthCondition(hc);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			req.setAttribute("user", user);
			req.getRequestDispatcher("/jsp/healthConditionSetting.jsp")
					.forward(req, resp);
		}
	}
}
